<?php
/**
 * Created by PhpStorm.
 * User: sunbo
 * Date: 2019/7/5
 * Time: 3:51 PM
 */
namespace app\index\controller;
use think\Exception;
class CoinBaseContorller extends BaseContorller
{
    public function  GetAddressFromUserAndCoinGuid($UserGuid,$CoinGuid){
        $row= $this->db_select("select address from token_user_coin_address WHERE token_sys_user_guid='".$UserGuid."' and token_sys_coin_guid='".$CoinGuid."'");
        return $row[0]["address"];
    }

//获取币余额
    public  function  GetCoinCount($CoinGuid,$UserGuid){
        $row=$this->db_select("select count(*) counts from token_sys_coin where guid='".$CoinGuid."' and iscustom=1");
        if($row[0]["counts"]>0){
           $count=$this->GetUserCustomCoinCount($UserGuid);
        }
        else{
            $count=$this->GetUserCoinCount($UserGuid,$CoinGuid);
        }
        return $count;
    }

    //获取矿工费
    public  function  GetCoinFee($CoinGuid){
       $row= $this->db_select("select fee from token_sys_coin_fee WHERE token_sys_coin_guid='".$CoinGuid."'");
        return $row[0]["fee"];
    }

    public  function  CheckTransactionPwd($UserGuid,$tr_Pwd){
       $counts= $this->db_select("SELECT count(*) counts from token_sys_user where guid='".$UserGuid."' and transaction_pwd='".$tr_Pwd."'");
       if($counts[0]["counts"]==0){
           return false;
       }else{
           return true;
       }
    }

    public function  GetCoinInfo($type){
        $row=null;
        switch ($type){
            case "0":
                $row=$this->db_select("SELECT guid,name,iscustom from token_sys_coin");
                break;
            case "1":
                $row=$this->db_select("SELECT guid,name,iscustom from token_sys_coin where iscustom=1");
                break;
            case "2":
                $row=$this->db_select("SELECT guid,name,iscustom from token_sys_coin where iscustom=0");
                break;
        }
        return $row;
    }

    //获取代币的余额
    public function GetUserCustomCoinCount($userid){
//        $row=$this->GetCoinInfo("1");
//        $coinid=$row[0]["guid"];
//       $row=$this->db_select("SELECT ifnull(sum(tokencount),0) tokencount from token_bill_transaction_coin WHERE status='1' and type='0' and token_sys_coin_guid='".$coinid."' and token_sys_user_guid='".$userid."'");
//       //买入
//       $purchasein_count=$row[0]["tokencount"];
//
//       $row=$this->db_select("SELECT ifnull(sum(result_token_count),0) tokencount from token_bill_exchange_coin WHERE  target_token_sys_coin_guid='".$coinid."' and token_sys_user_guid='".$userid."'");
//       //转入
//        $exchangein_count=$row[0]["tokencount"];
//
//        $row=$this->db_select("SELECT ifnull(sum(earnings_tokencount),0) tokencount from token_bill_earnings WHERE   token_sys_user_guid='".$userid."'");
//        //收益
//        $earnings_count=$row[0]["tokencount"];
//
//       $row=$this->db_select("SELECT ifnull(sum(tokencount),0) tokencount from token_bill_transaction_coin WHERE status='1' and type='1' and token_sys_coin_guid='".$coinid."' and token_sys_user_guid='".$userid."'");
//       //卖出
//       $sellout_count=$row[0]["tokencount"];
//
//        $row=$this->db_select("SELECT ifnull(sum(putin_token_count),0) tokencount from token_bill_exchange_coin WHERE  src_token_sys_coin_guid='".$coinid."' and token_sys_user_guid='".$userid."'");
//        //转出
//        $exchangeout_count=$row[0]["tokencount"];

//        return $purchasein_count+$exchangein_count+$earnings_count-$sellout_count-$exchangeout_count;
        $row=$this->db_select("select token_count from token_user_custom_coin_balance WHERE token_sys_user_guid='".$userid."'");
        return $row[0]["token_count"];
    }


    //获取其他币种的余额
    public function GetUserCoinCount($userid,$coinid){

        $row=$this->db_select("SELECT ifnull(sum(tokencount),0) tokencount from token_bill_transaction_coin WHERE status =1 and type='0' and token_sys_coin_guid='".$coinid."' and token_sys_user_guid='".$userid."'");
        //买入
        $purchasein_count=$row[0]["tokencount"];

        $row=$this->db_select("SELECT ifnull(sum(result_token_count),0) tokencount from token_bill_exchange_coin WHERE  target_token_sys_coin_guid='".$coinid."' and token_sys_user_guid='".$userid."'");
        //转入
        $exchangein_count=$row[0]["tokencount"];

        $row=$this->db_select("SELECT ifnull(sum(tokencount),0) tokencount from token_bill_transaction_coin WHERE status in (0,1,2) and type='1' and token_sys_coin_guid='".$coinid."' and token_sys_user_guid='".$userid."'");
        //卖出
        $sellout_count=$row[0]["tokencount"];

        $row=$this->db_select("SELECT ifnull(sum(putin_token_count),0) tokencount from token_bill_exchange_coin WHERE  src_token_sys_coin_guid='".$coinid."' and token_sys_user_guid='".$userid."'");
        //转出
        $exchangeout_count=$row[0]["tokencount"];

        return $purchasein_count+$exchangein_count-$sellout_count-$exchangeout_count;
    }

    //判断是否是代币
    public function  IsCustomCoin($coinguid){
       $row= $this->db_select("SELECT count(*) counts from token_sys_coin where guid='".$coinguid."' and iscustom=1");
           if($row[0]["counts"]>0){
               return true;
           }
           return false;
    }

    public function GetRSA($data){
        $encrypted=urldecode($data);
        $this->log($encrypted);
        $private_key_path = ROOT_PATH.'yangzie_private.pem';
        $public_key_path = ROOT_PATH.'yangzie_public.pem';
        $private_key = file_get_contents($private_key_path);
        $public_key = file_get_contents($public_key_path);
//判断私钥是否是可用的
        $pi_key = openssl_pkey_get_private($private_key);
//判断公钥是否是可用的
        $pu_key = openssl_pkey_get_public($public_key);
//原始数据

// 解密后的数据
        $decrypted = "";

//        openssl_public_encrypt($data, $encrypted, $pu_key);
//        $encrypted = base64_encode($encrypted);
////私钥加密，也可使用openssl_public_encrypt公钥加密，然后使用openssl_private_decrypt解密，加密后数据在$encrypted
//        openssl_private_encrypt($data, $encrypted, $pi_key);
////加密后的内容通常含有特殊字符，需要编码转换下，在网络间通过url传输时要注意base64编码是否是url安全的
//        $encrypted = base64_encode($encrypted);
//        dump($encrypted);
//私钥加密的内容通过公钥可解密出来,公钥加密的可用私钥解密。不能混淆
        openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key);
        return $decrypted;
    }


    public function Insert_token_bill_mm_report(){
        $curTime=date('Y-m-d H:i:s',strtotime('-1 minute'));
        $datetime=date('Y-m-d H:i',strtotime('-1 minute'));
        $timestamp=strtotime($curTime);
        $date=date('Y-m-d',time());

        $sqlwhere=" WHERE src_token_sys_coin_guid='2' and billdatetime like '".$datetime."%' ";
        $amount=0;
        $count=0;
        $open=0;
        $close=0;
        $low=0;
        $high=0;
        $vol=0;

        $row=$this->db_select("select  ifnull(sum(putin_token_count)/pow(10,8),0) amount  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $amount=$row[0]["amount"];
        }

        $row=$this->db_select("select count(*) counts  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $count=$row[0]["counts"];
        }

        $row=$this->db_select("select ifnull(ratio,0) ratio from token_bill_exchange_coin ".$sqlwhere." ORDER BY billdatetime ASC LIMIT 0,1");
        if(count($row)>0){
            $open=$row[0]["ratio"];
        }

        $row=$this->db_select("select ifnull(ratio,0) ratio from token_bill_exchange_coin ".$sqlwhere." ORDER BY billdatetime DESC LIMIT 0,1");
        if(count($row)>0){
            $close=$row[0]["ratio"];
        }


        $row=$this->db_select("select ifnull(MIN(ratio),0) ratio from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $low=$row[0]["ratio"];
        }

        $row=$this->db_select("select ifnull(MAX(ratio),0) ratio from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $high=$row[0]["ratio"];
        }

        $row=$this->db_select("select  ifnull(sum(result_token_count)/pow(10,8),0) amount  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $vol=$row[0]["amount"];
        }

            $this->db_exec(array("insert into token_bill_mm_report (timestamp_id,billdatetime,amount,count,open,close,low,high,vol,billdate)
 VALUES(".$timestamp.",'".$datetime."',".$amount.",".$count.",".$open.",".$close.",".$low.",".$high.",".$vol.",'".$date."')"));
    }


    function msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }


    public function Insert_token_bill_dd_report(){
        $date=date('Y-m-d',strtotime('-1 day'));
        $timestamp=strtotime($date);
        $sqlwhere=" WHERE src_token_sys_coin_guid='2' and billdate = '".$date."' ";
        $amount=0;
        $count=0;
        $open=0;
        $close=0;
        $low=0;
        $high=0;
        $vol=0;

        $row=$this->db_select("select  ifnull(sum(putin_token_count)/pow(10,8),0) amount  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $amount=$row[0]["amount"];
        }

        $row=$this->db_select("select count(*) counts  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $count=$row[0]["counts"];
        }

        $row=$this->db_select("select ifnull(ratio,0) ratio from token_bill_exchange_coin ".$sqlwhere." ORDER BY billdatetime ASC LIMIT 0,1");
        if(count($row)>0){
            $open=$row[0]["ratio"];
        }

        $row=$this->db_select("select ifnull(ratio,0) ratio from token_bill_exchange_coin ".$sqlwhere." ORDER BY billdatetime DESC LIMIT 0,1");
        if(count($row)>0){
            $close=$row[0]["ratio"];
        }


        $row=$this->db_select("select ifnull(MIN(ratio),0) ratio from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $low=$row[0]["ratio"];
        }

        $row=$this->db_select("select ifnull(MAX(ratio),0) ratio from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $high=$row[0]["ratio"];
        }

        $row=$this->db_select("select  ifnull(sum(result_token_count)/pow(10,8),0) amount  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $vol=$row[0]["amount"];
        }

            $this->db_exec(array("insert into token_bill_dd_report (timestamp_id,amount,count,open,close,low,high,vol,billdate)
 VALUES(".$timestamp.",".$amount.",".$count.",".$open.",".$close.",".$low.",".$high.",".$vol.",'".$date."')"));

    }


    public function Insert_token_bill_WW_report(){
        //结束日期
        $enddate= date('Y-m-d', strtotime('-1 saturday', time()));
        //起始日期
        $startdate= date('Y-m-d', strtotime('-2 sunday', time()));

        $timestamp=strtotime($enddate);
        $sqlwhere=" WHERE src_token_sys_coin_guid='2' and billdate between '".$startdate."' and '".$enddate."' ";
        $amount=0;
        $count=0;
        $open=0;
        $close=0;
        $low=0;
        $high=0;
        $vol=0;

        $row=$this->db_select("select  ifnull(sum(putin_token_count)/pow(10,8),0) amount  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $amount=$row[0]["amount"];
        }

        $row=$this->db_select("select count(*) counts  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $count=$row[0]["counts"];
        }

        $row=$this->db_select("select ifnull(ratio,0) ratio from token_bill_exchange_coin ".$sqlwhere." ORDER BY billdatetime ASC LIMIT 0,1");
        if(count($row)>0){
            $open=$row[0]["ratio"];
        }

        $row=$this->db_select("select ifnull(ratio,0) ratio from token_bill_exchange_coin ".$sqlwhere." ORDER BY billdatetime DESC LIMIT 0,1");
        if(count($row)>0){
            $close=$row[0]["ratio"];
        }


        $row=$this->db_select("select ifnull(MIN(ratio),0) ratio from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $low=$row[0]["ratio"];
        }

        $row=$this->db_select("select ifnull(MAX(ratio),0) ratio from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $high=$row[0]["ratio"];
        }

        $row=$this->db_select("select  ifnull(sum(result_token_count)/pow(10,8),0) amount  from token_bill_exchange_coin ".$sqlwhere);
        if(count($row)>0){
            $vol=$row[0]["amount"];
        }

        $this->db_exec(array("insert into token_bill_ww_report (timestamp_id,amount,count,open,close,low,high,vol,billdate)
 VALUES(".$timestamp.",".$amount.",".$count.",".$open.",".$close.",".$low.",".$high.",".$vol.",'".$enddate."')"));

    }



}